package com.cuzofu.fs.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cuzofu.fs.common.PageUtils;
import com.cuzofu.fs.common.Query;
import com.cuzofu.fs.common.exception.RRException;
import com.cuzofu.fs.dao.IssueDao;
import com.cuzofu.fs.entity.BlueprintHistoryEntity;
import com.cuzofu.fs.entity.IssueEntity;
import com.cuzofu.fs.entity.MarkEntity;
import com.cuzofu.fs.po.IssuePO;
import com.cuzofu.fs.service.BlueprintHistoryService;
import com.cuzofu.fs.service.IssueService;
import com.cuzofu.fs.service.MarkService;
import com.cuzofu.fs.vo.IssueVO;
import com.cuzofu.fs.vo.MarkVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("issueService")
public class IssueServiceImpl extends ServiceImpl<IssueDao, IssueEntity> implements IssueService {

    @Autowired
    private MarkService markService;

    @Autowired
    private BlueprintHistoryService blueprintHistoryService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<IssueEntity> page = this.page(
                new Query<IssueEntity>().getPage(params),
                new QueryWrapper<IssueEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<IssueVO> queryList(IssuePO po) {

        QueryWrapper<IssuePO> qw = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(po.getBhid())) {
            qw.eq("bhid", po.getBhid());
            qw.orderByDesc("create_time");
        }
        return this.baseMapper.queryIssueList(qw);
    }

    @Override
    @Transactional
    public IssueEntity createIssue(IssueVO vo) {

        String bhid = vo.getBhid();
        BlueprintHistoryEntity blueprintHistory = blueprintHistoryService.getById(bhid);
        if (blueprintHistory == null) {
            throw new RRException("记录不存在，操作失败！");
        }

        IssueEntity entity = new IssueEntity();
        entity.setPid(blueprintHistory.getPid());
        entity.setMid(blueprintHistory.getMid());
        entity.setUid(blueprintHistory.getUid());
        entity.setBid(blueprintHistory.getBid());
        entity.setBhid(vo.getBhid());
        entity.setOpinion(vo.getOpinion());
        this.save(entity);
        List<MarkVO> marks = vo.getMarks();
        if (marks != null) {
            List<MarkEntity> entities = new ArrayList<>();
            marks.forEach((m) -> {
                MarkEntity mark = new MarkEntity();
                mark.setIssueId(entity.getId());
                mark.setType(m.getType());
                mark.setData(m.getData());
                entities.add(mark);
            });
            markService.saveBatch(entities);
        }

        return entity;
    }

}